/*     */ package com.zimbra.cs.dav.resource;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavContext.RequestProp;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.DavProtocol.Compliance;
/*     */ import com.zimbra.cs.dav.property.Acl;
/*     */ import com.zimbra.cs.dav.property.ResourceProperty;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.Calendar;
/*     */ import java.util.Collection;
/*     */ import java.util.Collections;
/*     */ import java.util.Date;
/*     */ import java.util.GregorianCalendar;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.TimeZone;
/*     */ import java.util.TreeSet;
/*     */ import org.dom4j.DocumentHelper;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ import org.dom4j.io.OutputFormat;
/*     */ import org.dom4j.io.XMLWriter;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class DavResource
/*     */ {
/*     */   protected String mUri;
/*     */   protected String mOwner;
/*     */   protected Map<QName, ResourceProperty> mProps;
/*     */   protected Collection<DavProtocol.Compliance> mDavCompliance;
/*     */   protected boolean mNewlyCreated;
/*     */   
/*     */   public static class InvalidResource
/*     */     extends DavResource
/*     */   {
/*     */     public InvalidResource(String uri, String owner)
/*     */     {
/*  69 */       super(owner);
/*     */     }
/*     */     
/*     */     public void delete(DavContext ctxt) {}
/*     */     
/*  74 */     public boolean isCollection() { return false; }
/*     */     
/*  76 */     public boolean isValid() { return false; }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public DavResource(String uri, String owner)
/*     */   {
/*  86 */     this.mNewlyCreated = false;
/*  87 */     this.mOwner = owner;
/*  88 */     this.mProps = new HashMap();
/*  89 */     this.mUri = uri;
/*  90 */     if ((isCollection()) && (!this.mUri.endsWith("/")))
/*  91 */       this.mUri += "/";
/*  92 */     this.mDavCompliance = new TreeSet();
/*  93 */     this.mDavCompliance.add(DavProtocol.Compliance.one);
/*  94 */     this.mDavCompliance.add(DavProtocol.Compliance.two);
/*  95 */     this.mDavCompliance.add(DavProtocol.Compliance.three);
/*  96 */     this.mDavCompliance.add(DavProtocol.Compliance.access_control);
/*  97 */     this.mDavCompliance.add(DavProtocol.Compliance.calendar_access);
/*  98 */     this.mDavCompliance.add(DavProtocol.Compliance.version_control);
/*  99 */     this.mDavCompliance.add(DavProtocol.Compliance.calendar_proxy);
/* 100 */     this.mDavCompliance.add(DavProtocol.Compliance.calendarserver_principal_property_search);
/* 101 */     this.mDavCompliance.add(DavProtocol.Compliance.addressbook);
/* 102 */     this.mDavCompliance.add(DavProtocol.Compliance.extended_mkcol);
/* 103 */     if (isSchedulingEnabled()) {
/* 104 */       if (isCalendarAutoSchedulingEnabled()) {
/* 105 */         this.mDavCompliance.add(DavProtocol.Compliance.calendar_auto_schedule);
/*     */       } else {
/* 107 */         this.mDavCompliance.add(DavProtocol.Compliance.calendar_schedule);
/*     */       }
/*     */     }
/*     */     
/* 111 */     ResourceProperty rtype = new ResourceProperty(DavElements.E_RESOURCETYPE);
/* 112 */     rtype.setProtected(true);
/* 113 */     rtype.setVisible(true);
/* 114 */     addProperty(rtype);
/*     */     
/* 116 */     ResourceProperty href = new ResourceProperty(DavElements.E_HREF);
/* 117 */     href.setProtected(true);
/*     */     try {
/* 119 */       href.setStringValue(UrlNamespace.getResourceUrl(this));
/*     */     } catch (Exception e) {
/* 121 */       ZimbraLog.dav.error("can't generate href", e);
/*     */     }
/* 123 */     addProperty(href);
/* 124 */     if (hasEtag())
/* 125 */       setProperty(DavElements.E_GETETAG, getEtag(), true);
/* 126 */     if (isCollection())
/* 127 */       addResourceType(DavElements.E_COLLECTION);
/* 128 */     addProperty(Acl.getPrincipalCollectionSet());
/* 129 */     addProperty(Acl.getCurrentUserPrincipal());
/* 130 */     QName[] supportedReports = getSupportedReports();
/* 131 */     if (supportedReports.length > 0) {
/* 132 */       ResourceProperty sr = new ResourceProperty(DavElements.E_SUPPORTED_REPORT_SET);
/* 133 */       addProperty(sr);
/* 134 */       sr.setProtected(true);
/* 135 */       sr.setVisible(true);
/* 136 */       Element e = null;
/* 137 */       for (QName n : supportedReports) {
/* 138 */         e = DocumentHelper.createElement(DavElements.E_SUPPORTED_REPORT);
/* 139 */         e.addElement(DavElements.E_REPORT).addElement(n);
/* 140 */         sr.getChildren().add(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void setHref(String href) {
/* 146 */     getProperty(DavElements.E_HREF).setStringValue(HttpUtil.urlEscape(href));
/*     */   }
/*     */   
/*     */   public boolean isValid() {
/* 150 */     return true;
/*     */   }
/*     */   
/*     */   public boolean isNewlyCreated() {
/* 154 */     return this.mNewlyCreated;
/*     */   }
/*     */   
/*     */   public boolean equals(Object another)
/*     */   {
/* 159 */     if ((another instanceof DavResource)) {
/* 160 */       DavResource that = (DavResource)another;
/* 161 */       return (this.mUri.equals(that.mUri)) && (this.mOwner.equals(that.mOwner));
/*     */     }
/* 163 */     return false;
/*     */   }
/*     */   
/*     */   public Collection<DavProtocol.Compliance> getComplianceList() {
/* 167 */     return this.mDavCompliance;
/*     */   }
/*     */   
/*     */   public ResourceProperty getProperty(String propName) {
/* 171 */     return getProperty(QName.get(propName, DavElements.WEBDAV_NS));
/*     */   }
/*     */   
/*     */   public ResourceProperty getProperty(QName prop) {
/* 175 */     return (ResourceProperty)this.mProps.get(prop);
/*     */   }
/*     */   
/*     */   public ResourceProperty getProperty(Element prop) {
/* 179 */     if (prop == null)
/* 180 */       return null;
/* 181 */     return getProperty(prop.getQName());
/*     */   }
/*     */   
/*     */   public ResourceProperty getProperty(QName prop, DavContext.RequestProp request) {
/* 185 */     if (request.getProp(prop) == null)
/* 186 */       return getProperty(prop);
/* 187 */     return getProperty(request.getProp(prop));
/*     */   }
/*     */   
/*     */   public Set<QName> getAllPropertyNames() {
/* 191 */     Set<QName> ret = new HashSet();
/* 192 */     for (QName key : this.mProps.keySet()) {
/* 193 */       if (((ResourceProperty)this.mProps.get(key)).isVisible())
/* 194 */         ret.add(key);
/*     */     }
/* 196 */     return ret;
/*     */   }
/*     */   
/*     */   public String getUri() {
/* 200 */     return this.mUri;
/*     */   }
/*     */   
/*     */   public String getOwner() {
/* 204 */     return this.mOwner;
/*     */   }
/*     */   
/*     */   public boolean hasContent(DavContext ctxt) {
/*     */     try {
/* 209 */       return getContentLength() > 0;
/*     */     }
/*     */     catch (NumberFormatException e) {}
/* 212 */     return false;
/*     */   }
/*     */   
/*     */   public String getContentType(DavContext ctxt) {
/* 216 */     ResourceProperty prop = getProperty(DavElements.E_GETCONTENTTYPE);
/* 217 */     if (prop != null)
/* 218 */       return prop.getStringValue();
/* 219 */     return null;
/*     */   }
/*     */   
/*     */   public int getContentLength() {
/* 223 */     ResourceProperty prop = getProperty(DavElements.E_GETCONTENTLENGTH);
/* 224 */     if (prop != null)
/* 225 */       return Integer.parseInt(prop.getStringValue());
/* 226 */     return 0;
/*     */   }
/*     */   
/*     */   protected void setCreationDate(long ts) {
/* 230 */     setProperty("creationdate", DateUtil.toISO8601(new Date(ts)));
/*     */   }
/*     */   
/*     */   protected void setLastModifiedDate(long ts) {
/* 234 */     Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
/* 235 */     cal.setTimeInMillis(ts);
/* 236 */     setProperty("getlastmodified", DateUtil.toRFC1123Date(cal));
/*     */   }
/*     */   
/*     */   protected void addProperty(ResourceProperty prop) {
/* 240 */     this.mProps.put(prop.getName(), prop);
/*     */   }
/*     */   
/*     */   protected void addProperties(Set<ResourceProperty> props) {
/* 244 */     for (ResourceProperty p : props)
/* 245 */       this.mProps.put(p.getName(), p);
/*     */   }
/*     */   
/*     */   protected void setProperty(String key, String val) {
/* 249 */     setProperty(QName.get(key, DavElements.WEBDAV_NS), val);
/*     */   }
/*     */   
/*     */   protected void setProperty(QName key, String val) {
/* 253 */     setProperty(key, val, false);
/*     */   }
/*     */   
/*     */   protected void setProperty(QName key, String val, boolean isProtected) {
/* 257 */     ResourceProperty prop = (ResourceProperty)this.mProps.get(key);
/* 258 */     if (prop == null) {
/* 259 */       prop = new ResourceProperty(key);
/* 260 */       this.mProps.put(key, prop);
/*     */     }
/* 262 */     prop.setProtected(isProtected);
/* 263 */     prop.setStringValue(val);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean isAccessControlled()
/*     */   {
/* 270 */     return true;
/*     */   }
/*     */   
/*     */   public InputStream getContent(DavContext ctxt) throws IOException, DavException {
/* 274 */     return new ByteArrayInputStream(getTextContent(ctxt).getBytes("UTF-8"));
/*     */   }
/*     */   
/*     */   public abstract boolean isCollection();
/*     */   
/*     */   public abstract void delete(DavContext paramDavContext) throws DavException;
/*     */   
/*     */   public String getName() {
/* 282 */     return getUri();
/*     */   }
/*     */   
/*     */   public Collection<DavResource> getChildren(DavContext ctxt) throws DavException {
/* 286 */     return Collections.emptyList();
/*     */   }
/*     */   
/*     */   public boolean hasEtag() {
/* 290 */     return false;
/*     */   }
/*     */   
/* 293 */   public String getEtag() { return null; }
/*     */   
/*     */   public String getLastModifiedDate()
/*     */   {
/* 297 */     ResourceProperty rp = getProperty("getlastmodified");
/* 298 */     if (rp != null)
/* 299 */       return rp.getStringValue();
/* 300 */     return new Date(0L).toString();
/*     */   }
/*     */   
/*     */   public void patchProperties(DavContext ctxt, Collection<Element> set, Collection<QName> remove) throws DavException, IOException {
/* 304 */     throw new DavException("PROPPATCH not supported on " + getUri(), 424, null);
/*     */   }
/*     */   
/*     */   protected void addResourceType(QName type) {
/* 308 */     ResourceProperty rtype = getProperty(DavElements.E_RESOURCETYPE);
/* 309 */     rtype.addChild(type);
/*     */   }
/*     */   
/*     */   public void handlePost(DavContext ctxt) throws DavException, IOException, ServiceException {
/* 313 */     throw new DavException("the resource does not handle POST", 403);
/*     */   }
/*     */   
/*     */   public boolean isLocal() {
/* 317 */     return true;
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 322 */     return this.mUri;
/*     */   }
/*     */   
/* 325 */   public DavResource copy(DavContext ctxt, DavResource dest) throws DavException { throw new DavException("not supported", 406); }
/*     */   
/*     */   protected QName[] getSupportedReports()
/*     */   {
/* 329 */     return new QName[0];
/*     */   }
/*     */   
/*     */   protected String getTextContent(DavContext ctxt) throws IOException {
/* 333 */     StringBuilder buf = new StringBuilder();
/* 334 */     buf.append("Request\n\n");
/* 335 */     buf.append("\tAuthenticated user:\t").append(ctxt.getAuthAccount().getName()).append("\n");
/* 336 */     buf.append("\tCurrent date:\t\t").append(new Date(System.currentTimeMillis())).append("\n");
/* 337 */     buf.append("\nResource\n\n");
/* 338 */     buf.append("\tName:\t\t\t").append(getName()).append("\n");
/* 339 */     buf.append("\tPath:\t\t\t").append(getUri()).append("\n");
/* 340 */     buf.append("\tDate:\t\t\t").append(getLastModifiedDate()).append("\n");
/*     */     try {
/* 342 */       Provisioning prov = Provisioning.getInstance();
/* 343 */       Account account = prov.get(Key.AccountBy.name, getOwner());
/* 344 */       buf.append("\tOwner account name:\t").append(account.getName()).append("\n");
/*     */     }
/*     */     catch (ServiceException se) {}
/* 347 */     buf.append("\nProperties\n\n");
/* 348 */     buf.append(getPropertiesAsText(ctxt));
/* 349 */     return buf.toString();
/*     */   }
/*     */   
/*     */   protected String getPropertiesAsText(DavContext ctxt) throws IOException {
/* 353 */     Element e = DocumentHelper.createElement(DavElements.E_PROP);
/* 354 */     for (ResourceProperty rp : this.mProps.values())
/* 355 */       rp.toElement(ctxt, e, false);
/* 356 */     OutputFormat format = OutputFormat.createPrettyPrint();
/* 357 */     format.setTrimText(false);
/* 358 */     format.setOmitEncoding(false);
/* 359 */     ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* 360 */     XMLWriter writer = new XMLWriter(baos, format);
/* 361 */     writer.write(e);
/* 362 */     return new String(baos.toByteArray());
/*     */   }
/*     */   
/*     */   public static boolean isSchedulingEnabled() {
/*     */     try {
/* 367 */       return !Provisioning.getInstance().getConfig().getBooleanAttr("zimbraCalendarCalDavDisableScheduling", false);
/*     */     }
/*     */     catch (ServiceException se) {}
/* 370 */     return false;
/*     */   }
/*     */   
/*     */   public static boolean isCalendarAutoSchedulingEnabled()
/*     */   {
/* 375 */     if (!isSchedulingEnabled()) {
/* 376 */       return false;
/*     */     }
/*     */     try {
/* 379 */       return Provisioning.getInstance().getConfig().getBooleanAttr("zimbraCalendarCalDavCalendarAutoScheduleEnabled", true);
/*     */     }
/*     */     catch (ServiceException se) {}
/* 382 */     return true;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/resource/DavResource.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */